package com.kaka.bus.engine.home.info.coupon;


import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.adapter.SelfPageAdapter;
import com.kaka.bus.base.SimpleActivity;
import com.kaka.bus.engine.home.info.coupon.page.UnUseCouponFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * Created by zhangqi on 2016/12/2.
 */

public class CouponActivity extends SimpleActivity {


    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.bg_coupon_unuser)
    RadioButton mBgCouponUnuser;
    @BindView(R.id.bg_coupon_nouser)
    RadioButton mBgCouponNouser;
    @BindView(R.id.rg_coupon)
    RadioGroup mRgCoupon;
    @BindView(R.id.vp_coupon)
    ViewPager mVpCoupon;
    List<Fragment> mCouponPageList;

    @Override
    protected void initView() {
        mToolbarBack.setOnClickListener(v -> finish());
        mToolbarName.setText("优惠券");

        mRgCoupon.check(R.id.bg_coupon_unuser);
        if (mCouponPageList == null)
            mCouponPageList = new ArrayList<>();

        mCouponPageList.add(UnUseCouponFragment.getInstance(UnUseCouponFragment.UNUSE));
        mCouponPageList.add(UnUseCouponFragment.getInstance(UnUseCouponFragment.LOSE));

        mVpCoupon.setAdapter(new SelfPageAdapter(getSupportFragmentManager(), mCouponPageList));
    }

    @Override
    protected void initListener() {
        /**
         * viewpager的监听 点击viewpager改变radiogroup
         */
        mVpCoupon.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                int check = R.id.bg_coupon_unuser;
                switch (position) {
                    case 0:
                        check = R.id.bg_coupon_unuser;
                        break;
                    case 1:
                        check = R.id.bg_coupon_nouser;
                        break;
                }
                mRgCoupon.check(check);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        /**
         * radiogroup的监听 点击radiogroup改变viewpager
         */
        mRgCoupon.setOnCheckedChangeListener((group, checkedId) -> {
            int check = 0;
            switch (checkedId) {
                case R.id.bg_coupon_unuser:
                    check = 0;
                    break;
                case R.id.bg_coupon_nouser:
                    check = 1;
                    break;

            }
            mVpCoupon.setCurrentItem(check);
        });


    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_coupon;
    }

}
